clear
clear mata
clear matrix
set maxvar 10000
estimates clear

loc events "wneg2 any_rlost_job male_rwidowed fem_rwidowed any_rhosp any_disab any_health3to5 any_rcancr any_rstrok any_rcardiac any_rmemory"

//load parent file
use data/constructed_data/hrs_analysis_respondents.dta if wave>=2 , clear  
rename avg_real_ktcamt amt_pk
rename avg_real_kfcamt amt_kp
capture drop khlphrs* any_helpr amt_kp* amt_pk* at_pk at_kp* fkh_apk
/* merge on transfer files */
merge 1:1 hhidpn wave using data/constructed_data/transfers.dta
keep if _merge==3
drop _merge

label var wave "Survey Wave"
label var at_pk "Parent-to-child transfer"
label var at_kp "Child-to-parent transfer"
label var amt_pk "Parent to child amt, with 0s"
label var amt_kp "Child to parent amt, with 0s"
label var amt_pk_pos "Parent-to-child amt, nonzero"
label var amt_kp_pos "Child-to-parent amt, nonzero"
label var any_helpr "Any child helps"
label var help_hrs "Help Hours, with 0s"
label var help_hrs_pos "Help Hours, nonzero"

rename frac_kh_amtpk fkh_apk // this is the fraction of giving to helper kids

cap drop at_pk_hh at_kp_hh  

tostring hhidpn, gen(temp)
gen pn = substr(temp,-3, 3)
destring pn, replace
gen hhid = hhidpn - pn
drop pn temp

label var hhidpn "Person ID"
label var hhid "Household ID"


//Set Panel
xtset hhidpn wave
bys hhidpn: egen hhwaves = sum(wave!=.)
tab hhwaves
bysort hhidpn: egen weight = mean(rwthh)
drop if weight==0  /*59 obs*/

label var weight "Constant Weight"

//Prepare variables

//Wealth Shocks
gen wpos = (real_hatota > 1.5*l.real_hatota) & l.real_hatota>20000 & l.real_hatota!=.
gen wneg = (real_hatota < .75*l.real_hatota) & l.real_hatota>20000 & l.real_hatota!=.
gen wneg2 = wneg
replace wneg2 = 0 if l.wpos==1
replace wneg2 = 0 if l2.wpos==1
drop wpos wneg
	
//Life Expectancy
replace rliv75 = rliv85 if rliv75==.
replace rliv75 = rliv10 if rliv75==.

//Age and Race
egen ageXwave = group(oldest_age wave)
gen oldest_age2 = oldest_age^2
drop if any_nonwhite==.  /*174,318 household-waves, 19,439 households*/

//Transfers
gen any_lvtenmi = cond(nk_lvtenmi > 0, 1, 0)

//Income, Wealth, Medical Expenditures
gen any_work = rwork==1 | swork==1
xtile inc_quintile_pc = real_hitot_pc , nq(5)
tab inc_quintile_pc, gen(iq)
xtile wealth_quintile_pc = real_hatota_pc , nq(5)
tab wealth_quintile_pc, gen(wq)
gen ln_inc = ln(real_hitot_pc+1)
gen ln_wealth = ln(real_hatota_pc+1)
gen wlthpc = real_hatota_pc
gen incpc = real_hitot_pc
gen medicare_eligible = oldest_age>= 65
gen oop = real_hoopmd
gen real_hoopmd_1000 = real_hoopmd/1000
su real_hoopmd, det
gen hi_oopmd = real_hoopmd>=r(p90)
egen avg_lifetime_wealth_pc = mean(real_hatota_pc), by(hhidpn)
su avg_lifetime_wealth_pc, detail
gen medwealth = r(p50)

//Education
gen hsgrad = (highest_ed_yr >= 12 & highest_ed_yr < 16)
gen college = highest_ed_yr>=16

//Household
gen ris_female = cond(ragender == 2, 1, 0)
gen single_female = ris_female == 1 & ris_married == 0
gen single_male = ris_female == 0 & ris_married == 0
gen fem_rwidowed = any_rwidowed-male_rwidowed
gen kidcat = 1 if nkids==1
replace kidcat = 2 if nkids==2 
replace kidcat = 3 if nkids>=3 
replace kidcat = 0 if nkids==0

//Health shocks
rename any_rstrok_dummy any_rstrok

// Primary house imputation
gen noimp_hou = 1 if hafhous ==1 | hafhous == 6
replace noimp_hou = 0 if mi(noimp_hou) 


     label var wneg2 "Wealth Loss"
     label var any_rlost_job "Job Exit"
     label var male_rwidowed "Male Widowed"
     label var fem_rwidowed "Female Widowed"
     label var any_disab "Disability Onset"
     label var any_health3to5 "Poor Health"
     label var any_rhosp "Hospitalization"
     label var any_rcancr "Cancer"
     label var any_rstrok "Stroke" 
     label var any_rcardiac "Cardiac"
     label var any_rmemory "Memory"

	 label var real_hitot_pc "Real per capita income"
     label var real_hatota_pc "Real per capita assets"
     label var iq2 "Income Q2"
     label var iq3 "Income Q3"
     label var iq4 "Income Q4" 
     label var iq5 "Income Q5"
     label var wq2 "Wealth Q2"
     label var wq3 "Wealth Q3"
     label var wq4 "Wealth Q4"
     label var wq5 "Wealth Q5"
     label var wlthpc "Wealth, 100,000s"
     label var incpc "Income, 10,000s"
     label var ln_inc "Log income"
     label var ln_wealth "Log wealth"
	 
     label var rownhm "Home owner"
     label var any_ssi "SSI receipt"
     label var any_rhosp "Hospitalization"
     label var any_bad_health "Any bad health"
     label var any_disability "Any disability"
     label var single_male "Single male"
     label var single_female "Single female"
     label var any_lvtenmi "Any child within 10 miles"
     label var rliv75 "Life expectancy"
     label var rbeq100 "Probability of bequest"
     label var any_work "Any employed"
     label var oop "Out-of-pocket medical expenses"
     label var coresid "Live with child"

     label var oldest_age "Oldest age" 
     label var oldest_age2 "Oldest Age Sq"
     label var any_nonwhite "Any non-white"
     label var highest_ed "Highest education"
     label var hsgrad "High school"
     label var college "College"
     label var nkids2 "Number of children"

keep hhid* wave kapick rwthh at_* amt_* any_helpr help_* wneg2 *_rlost_job male_rwidowed fem_rwidowed any_disab any_health3to5 any_rhosp any_rcancr any_rstrok any_rcardiac any_rmemory iq* wq* wlthpc incpc highest_ed real_hitot_pc real_hatota_pc rownhm any_ssi any_rhosp any_bad_health any_disab any_disability single_male single_female any_lvtenmi rliv75 rbeq100 any_work coresid ln_inc ln_wealth oldest_age oldest_age2 any_nonwhite hsgrad college nkids2 weight *quintile* rshlt hhwaves kidcat any_rwidowed medwealth real_hoopmd oop nk_lvtenmi any_live_nrshm any_medicaid *rhomcar* any_jl* wshock* any_nrshmchange noimp_hou fkh_apk 
 
order hhid* wave at_* amt_* any_helpr help_* wneg2 any_rlost_job male_rwidowed fem_rwidowed any_disab any_health3to5 any_rhosp any_rcancr any_rstrok any_rcardiac any_rmemory
 
save data/constructed_data/analysisdata.dta, replace
